<?php
define('IN_ROSTER', true);

class apiguildUpdate
{
	var $guildID;
	var $data=array();
	var $upd;
	var $gname;
	var $gserver;
	var $region;
	var $faction = array(
	0 => 'Alliance',
	1 => 'Horde');
	var $factionEN = array(0 => 'Alliance',
	1 => 'Horde');
	var $raceidtofaction = array(
	1 => 0,
	2 => 1,
	3 => 0,
	4 => 0,
	5 => 1,
	6 => 1,
	7 => 0,
	8 => 1,
	10 => 1,
	11 => 0,
	22 => 0,
	9 => 1
);

	var $lang = array (
	'id_to_class' => array(
		1 => 'Warrior',
		2 => 'Paladin',
		3 => 'Hunter',
		4 => 'Rogue',
		5 => 'Priest',
		6 => 'Death Knight',
		7 => 'Shaman',
		8 => 'Mage',
		9 => 'Warlock',
		11 => 'Druid'
		),
	'race_to_en' => array(
		'Blood Elf' => 'Blood Elf',
		'Draenei'   => 'Draenei',
		'Night Elf' => 'Night Elf',
		'Dwarf'     => 'Dwarf',
		'Gnome'     => 'Gnome',
		'Human'     => 'Human',
		'Orc'       => 'Orc',
		'Undead'    => 'Undead',
		'Troll'     => 'Troll',
		'Tauren'    => 'Tauren',
		'Worgen'    => 'Worgen',
		'Goblin'    => 'Goblin'
		),
	'race_to_id' => array(
		'Human'     => 1,
		'Orc'       => 2,
		'Dwarf'     => 3,
		'Night Elf' => 4,
		'Undead'    => 5,
		'Tauren'    => 6,
		'Gnome'     => 7,
		'Troll'     => 8,
		'Blood Elf' => 10,
		'Draenei'   => 11,
		'Worgen'    => 22,
		'Goblin'    => 9
		),
	'id_to_race' => array(
		1 => 'Human',
		2 => 'Orc',
		3 => 'Dwarf',
		4 => 'Night Elf',
		5 => 'Undead',
		6 => 'Tauren',
		7 => 'Gnome',
		8 => 'Troll',
		10 => 'Blood Elf',
		11 => 'Draenei',
		22 => 'Worgen',
		9 => 'Goblin'
		),
	);
	/*
	$db =& JFactory::getDBO();
	$query = "SELECT * FROM #__guildroster_charinfo ORDER BY name";
	$db->setQuery($query);
	$rows = $db->loadObjectList();
	*/
	
	function getguildid($n,$s)
	{

		$db =& JFactory::getDBO();
		$query = "SELECT * FROM #__guild_guilds WHERE guild_name = '$n' AND server = '$s'";
		$db->setQuery($query);
		$row = $db->loadAssoc();

		return $row['guild_id'];
	}
	function update_guild($data,$region)
	{
		$update = new _Update();
		$db =& JFactory::getDBO();
		
		$this->region = $region;
		$this->gname = $data['name'];
		$this->gserver = $data['realm'];
		$this->guildID = $this->getguildid($data['name'],$data['realm']);

		$g = array(
		'guild_id' => (isset($this->guildID) ? $this->guildID : ''),
		'guild_name' => $data['name'],
		'update_time' => time(),
		'region' => $this->region,
		'server' => $data['realm'],
		'faction' => $data['side'],
		'factionEn' => $this->faction[$data['side']],
		'guild_num_members' => count($data['members']),
		'guild_level' => $data['level']);
		$p = (object) $g;
		if( isset($this->guildID) )
		{
			$querystr = "UPDATE #__guild_guilds SET " . $update->assignstr . " WHERE `guild_id` = '".$this->guildID."';";
			$db->updateObject('#__guild_guilds', $p, 'guild_id', false); 
			$this->upd = true;
		}
		else
		{
			$querystr = "INSERT INTO #__guild_guilds SET " . $update->assignstr . ";";
			$db->insertObject('#__guild_guilds', $p);
		}
		echo $querystr.'<br>';
		//$ret = $db->setQuery($querystr);
		if( !isset($this->guildID) )
		{
			$this->guildID = $this->getguildid($data['name'],$data['realm']);
		}
		//echo '<br>--'.$this->guildID.'--<br>';
		//return true;
		$this->_processmembers($data['members']);
		return $data = array('name'=>$data['name'],'server'=>$data['realm'],'id'=>$this->guildID,'data'=>array($data));
	}
	
	
	function getmemid($n,$s)
	{

		$db =& JFactory::getDBO();
		$query = "SELECT * FROM #__guild_players WHERE name = '$n' AND server = '$s'";
		$db->setQuery($query);
		$row = $db->loadAssoc();

		return $row['member_id'];
	}
	function _processmembers($members)
	{
		$db =& JFactory::getDBO();
		$m = array();
		foreach ( $members as $id => $member )
		{
			$p = $mem = $member_id = null;
			
			//echo $roster->locale->wordings['enUS']['id_to_class'][$member['classid']];
			$member_id = $this->getMemid($member['character']['name'],$this->gserver);
			$mem = array(
			'member_id' => $member_id,
			'name' => $member['character']['name'],
			'guild_id' => $this->guildID,
			'dateupdatedutc' => time(),
			'guild' => $this->gname,
			'guild_rank' => $member['rank'],
			'race' => $this->lang['id_to_race'][$member['character']['race']],
			'raceid' => $member['character']['race'],
			'raceEn' => $this->lang['race_to_en'][$this->lang['id_to_race'][$member['character']['race']]],
			'class' => $this->lang['id_to_class'][$member['character']['class']],
			'classid' => $member['character']['class'],
			'classEn' => $this->lang['class_to_en'][$this->lang['id_to_class'][$member['character']['class']]],
			'factionID' => $this->raceidtofaction[$member['character']['race']],
			'factionEN' => $this->factionEN[$this->raceidtofaction[$member['character']['race']]],
			'sex' => $this->return_gender($member['character']['gender']),
			'sexid' => $member['character']['gender'],
			'level' => $member['character']['level'],
			'server' => $this->gserver,
			'region' => $this->region,
			'thumbnail' => $member['character']['thumbnail'],
			'achievementPoints' => $member['character']['achievementPoints']);
			$p = (object) $mem;
			if( isset($member_id) )
			{
				$querystr = "UPDATE #__guild_players SET " . $update->assignstr . " WHERE `guild_id` = '".$this->guildID."';";
				$db->updateObject('#__guild_players', $p, 'member_id', false); 
			}
			else
			{
				$querystr = "INSERT INTO #__guild_guilds SET " . $update->assignstr . ";";
				$db->insertObject('#__guild_players', $p);
			}
		
		}
	
	
	}
	
	
	function return_gender($genderid) {
		if ($genderid == "0") {$gender = "Male";}
		if ($genderid == "1") {$gender = "Female";}
		return $gender;
	}
	
	
	
	
}

?>